﻿@model ExamPlat.Models.ExamQuesItem
@{
    ViewBag.Title = "Edit";
}
<form action="/ExamQuesItem/Edit" method="post" onsubmit="return CheckForm()">
    <div style="float:left; width:400px;">
        <div>@Html.ActionLink("返回列表", "Index", new { searchTitle = ViewBag.SearchTitle, searchType = ViewBag.SearchType, searchExamID = ViewBag.SearchExamID })</div>
        @Html.HiddenFor(c => c.ID)
        <div class="editor-label">
            考题
        </div>
        <div class="editor-field">
            <select name="ExamQuesID" id="ExamQuesID" style="width:200px; height:25px;">
                @{
                    if (ViewBag.ExamQuesList != null)
                    {
                        IList<ExamPlat.Models.ExamQues> list = ViewBag.ExamQuesList as IList<ExamPlat.Models.ExamQues>;
                        foreach (var item in list)
                        {
                            if (ViewBag.SearchExamID == item.ID)
                            {
                                <option value="@item.ID" selected="selected">@item.ExamQuesTitle</option>
                            }
                            else
                            {
                                <option value="@item.ID">@item.ExamQuesTitle</option>
                            }
                        }
                    }
                }
            </select>
            <span id="spanExamQuesID" style="color:red; "></span>
        </div>
        <div class="editor-label">
            问题
        </div>
        <div class="editor-field">
            <input type="text" name="ExamQuesItemTitle" id="ExamQuesItemTitle" value="@Model.ExamQuesItemTitle" />
            <span id="spanExamQuesItemTitle" style="color:red; "></span>
        </div>
        <div class="editor-label">
            问题类型
        </div>
        <div class="editor-field">
            <select name="ExamQuesItemType" id="ExamQuesItemType" style="width:200px; height:25px;">
                @{
                    if (Model.ExamQuesItemType == 1)
                    {
                        <option value="1" selected="selected">单选题</option>
                    }
                    else
                    {
                        <option value="1">单选题</option>
                    }
                    if (Model.ExamQuesItemType == 2)
                    {
                        <option value="2" selected="selected">多选题</option>
                    }
                    else
                    {
                        <option value="2">多选题</option>
                    }
                    if (Model.ExamQuesItemType == 3)
                    {
                        <option value="3" selected="selected">对错题</option>
                    }
                    else
                    {
                        <option value="3">对错题</option>
                    }
                }
            </select>
        </div>
        <div class="editor-label">
            标准答案
        </div>
        <div class="editor-field">
            <input type="hidden" name="ExamQuesItemAnswer" id="ExamQuesItemAnswer" value="@Model.ExamQuesItemAnswer" />
            <input type="checkbox" value="A" id="ckA" /><span style="margin-left:5px; margin-right:15px;">A</span>
            <input type="checkbox" value="B" id="ckB" /><span style="margin-left:5px; margin-right:15px;">B</span>
            <input type="checkbox" value="C" id="ckC" /><span id="spanC" style="margin-left:5px; margin-right:15px;">C</span>
            <input type="checkbox" value="D" id="ckD" /><span id="spanD" style="margin-left:5px; margin-right:5px;">D</span>
            <span id="spanExamQuesItemAnswer" style="color:red; margin-left:25px; "></span>
        </div>
        <div style="margin-top:10px; margin-left:150px; "><input type="submit" value="保存" class="btn btn-primary" /></div>
        <div class="msg">@ViewBag.Msg</div>
        <div><input type="hidden" name="searchTitle" value="@ViewBag.SearchTitle" /></div>
        <div><input type="hidden" name="searchExamID" value="@ViewBag.SearchExamID" /></div>
        <div><input type="hidden" name="searchType" value="@ViewBag.SearchType" /></div>
    </div>
    <div style="float:left; padding-top:10px;" id="divQuesOptoin">
        问题选项<span id="spanMsg" style="color:red; margin-left:20px;"></span>
        <div id="divOption1" style="display:none;">
            @{
                if (Model.OptionList != null && Model.ExamQuesItemType != 3)
                {
                    List<ExamPlat.Models.ExamQuesItemOption> optionList = Model.OptionList.OrderBy(c => c.ExamQuesItemOptionNum).ToList();
                    <div style="margin:5px;"><label style="padding:5px;">A</label><input type="text" name="option1A" value="@optionList[0].ExamQuesItemOptionTitle" /></div>
                    <div style="margin:5px;">
                        <label style="padding:5px;">B</label><input type="text" name="option1B" value="@optionList[1].ExamQuesItemOptionTitle" />
                    </div>
                    <div style="margin:5px;">
                        <label style="padding:5px;">C</label><input type="text" name="option1C" value="@optionList[2].ExamQuesItemOptionTitle" />
                    </div>
                    <div style="margin:5px;"><label style="padding:5px;">D</label><input type="text" name="option1D" value="@optionList[3].ExamQuesItemOptionTitle" /></div>
                }
                else
                {
                    <div style="margin:5px;"><label style="padding:5px;">A</label><input type="text" name="option1A" /></div>
                    <div style="margin:5px;">
                        <label style="padding:5px;">B</label><input type="text" name="option1B" />
                    </div>
                    <div style="margin:5px;">
                        <label style="padding:5px;">C</label><input type="text" name="option1C" />
                    </div>
                    <div style="margin:5px;"><label style="padding:5px;">D</label><input type="text" name="option1D" /></div>
                }
            }
        </div>
        <div id="divOption2" style="display:none;">
            @{
                if (Model.OptionList != null && Model.ExamQuesItemType == 3)
                {
                    List<ExamPlat.Models.ExamQuesItemOption> optionList = Model.OptionList.OrderBy(c => c.ExamQuesItemOptionNum).ToList();
                    <div style="margin:5px;">
                        <label style="padding:5px;">A</label><input type="text" value="@optionList[0].ExamQuesItemOptionTitle" name="option2A" />
                    </div>
                    <div style="margin:5px;">
                        <label style="padding:5px;">B</label><input type="text" value="@optionList[1].ExamQuesItemOptionTitle" name="option2B" />
                    </div>
                }
                else
                {
                    <div style="margin:5px;">
                        <label style="padding:5px;">A</label><input type="text" value="对" name="option2A" />
                    </div>
                    <div style="margin:5px;">
                        <label style="padding:5px;">B</label><input type="text" value="错" name="option2B" />
                    </div>
                }
            }
        </div>
    </div>
</form>
<script type="text/javascript">
    $(function () {
        var answer = $("#ExamQuesItemAnswer").val();
        $("input[type=checkbox]").each(function () {
            if (answer.indexOf($(this).val()) != -1) {
                $(this).attr("checked", true);
            }
        });
        if ('@Model.ExamQuesItemType' == '3') {
            $("#divOption2").css("display", "block");
            $("#ckC").css("display", "none");
            $("#ckD").css("display", "none");
            $("#spanC").css("display", "none");
            $("#spanD").css("display", "none");
        }
        else {
            $("#divOption1").css("display", "block");
        }

        $("input[type=checkbox]").click(function () {
            var oldObj = $("#ExamQuesItemAnswer");
            var oldValue = $("#ExamQuesItemAnswer").val();
            if ($(this).is(":checked")) {
                if (oldValue.length == 0) {
                    oldObj.val($(this).val());
                }
                else {
                    oldObj.val(oldValue + "、" + $(this).val());
                }
            }
            else {
                oldValue = oldValue.replace($(this).val() + "、", "").replace("、" + $(this).val(), "").replace($(this).val(), "");
                oldObj.val(oldValue);
            }
        });

        $("#ExamQuesItemType").change(function () {
            $("input[type=checkbox]").each(function () {
                $(this).removeAttr("checked");
            });
            $("#ExamQuesItemAnswer").val("");
            var type = $(this).val();
            $("#spanMsg").text("");
            $("#spanExamQuesItemAnswer").text("");
            if (type == "3") {
                $("#divOption2").css("display", "block");
                $("#divOption1").css("display", "none");
                $("#divQuesOptoin").css("padding-top", "70px");
                $("#ckC").css("display", "none");
                $("#ckD").css("display", "none");
                $("#spanC").css("display", "none");
                $("#spanD").css("display", "none");
                $("#spanExamQuesItemAnswer").css("margin-left", "105px");

            }
            else {
                $("#divOption1").css("display", "block");
                $("#divOption2").css("display", "none");
                $("#divQuesOptoin").css("padding-top", "10px");
                $("#ckC").css("display", "inline");
                $("#ckD").css("display", "inline");
                $("#spanC").css("display", "inline");
                $("#spanD").css("display", "inline");
                $("#spanExamQuesItemAnswer").css("margin-left", "25px");
            }
        });
    });

    function CheckForm() {
        var flag = true;
        var tmpFlag = true;
        var examQuesID = $("#ExamQuesID").val();
        if (examQuesID == null || examQuesID.length == 0) {
            $("#spanExamQuesID").text("请选择一个考题");
            flag = false;
        }
        else {
            $("#spanExamQuesID").text("");
        }
        var examQuesItemTitle = $("#ExamQuesItemTitle").val();
        if (examQuesItemTitle.length == 0) {
            $("#spanExamQuesItemTitle").text("问题不能为空");
            flag = false;
        }
        else {
            $("#spanExamQuesItemTitle").text("");
        }
        var type = $("#ExamQuesItemType").val();
        if (type == 3) {
            $("#divOption2 input").each(function () {
                if ($(this).val().length == 0) {
                    tmpFlag = false;
                    return false;
                }
            });
        }
        else {
            $("#divOption1 input").each(function () {
                if ($(this).val().length == 0) {
                    tmpFlag = false;
                    return false;
                }
            });
        }
        if (!tmpFlag) {
            $("#spanMsg").text("问题选项不能为空");
            flag = false;
        }
        else {
            $("#spanMsg").text("");
        }
        if ($("#ExamQuesItemAnswer").val().length == 0) {
            $("#spanExamQuesItemAnswer").text("标准答案不能为空");
            flag = false;
        }
        else {
            var curValue = $("#ExamQuesItemAnswer").val();
            if (type == "1" || type == "3") {
                var tmpPlaceHolder = "";
                if (type == "1") {
                    tmpPlaceHolder = "单选题";
                }
                else if (type == "3") {
                    tmpPlaceHolder = "对错题";
                }
                if (curValue.indexOf("、") != -1) {
                    $("#spanExamQuesItemAnswer").text(tmpPlaceHolder + "标准答案只能有一个");
                    flag = false;
                }
                else {
                    $("#spanExamQuesItemAnswer").text("");
                }
            }
            else {
                $("#spanExamQuesItemAnswer").text("");
            }
        }
        return flag;
    }
</script>
